package com.pocketsweet.service;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.mobclick.android.MobclickAgent;
import com.pocketsweet.MLApplication;
import com.pocketsweet.chatlib.controller.MolianContactManager;
import com.pocketsweet.ui.More;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.MyAlertDialog;
import com.pocketsweet.utils.ToolKits;
import com.umeng.update.UmengDialogButtonListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

public class UpdateService {
	private Context ctx;
	public static LoadingDailog loading;
	private MLApplication mlApplication;
	private PackageManager manager;
	private PackageInfo info = null;

	public UpdateService(Context context, final boolean isAutoCheckUpdate) {
		this.ctx = context;
		if (isAutoCheckUpdate == false) { // 手动检测更新
			loading = ToolKits.createLoadingDialog(ctx, "提交中..");
			loading.setText("加载中");
			loading.show();
		}
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		mlApplication = MLApplication.getInstance();
		MobclickAgent.updateOnlineConfig(context);
		String upgrade_mode = MobclickAgent.getConfigParams(context,
				"upgrade_mode");
		if (TextUtils.isEmpty(upgrade_mode)) {
			if (isAutoCheckUpdate == false) {
				loading.dismiss();
			}
			return;
		}
		String[] upgrade_mode_array = upgrade_mode.split(";");
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(context);
		UmengUpdateAgent.forceUpdate(context);// 这行如果是强制更新就一定加上
		for (String mode : upgrade_mode_array) {
			String versionName = null;
			manager = ctx.getPackageManager();
			try {
				info = manager.getPackageInfo(ctx.getPackageName(), 0);
				versionName = info.versionName.toString();
			} catch (NameNotFoundException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();
			}
			versionName = versionName + "F";
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateOnlyWifi(false);
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {

				@Override
				public void onUpdateReturned(int updateStatus,
						UpdateResponse updateInfo) {
					if (isAutoCheckUpdate == false) {
						loading.dismiss();
					}
					switch (updateStatus) {
					case UpdateStatus.Yes: // has update
						UmengUpdateAgent.showUpdateDialog(ctx, updateInfo);
						break;
					case UpdateStatus.No: // has no update
						if (isAutoCheckUpdate == false) {
							Toast.makeText(ctx, "当前已经是最新版本", Toast.LENGTH_SHORT)
									.show();
						}
						break;
					case UpdateStatus.NoneWifi: // none wifi
						if (isAutoCheckUpdate == false) {
							Toast.makeText(ctx, "没有wifi连接， 只在wifi下更新",
									Toast.LENGTH_SHORT).show();
						}
						break;
					case UpdateStatus.Timeout: // time out
						if (isAutoCheckUpdate == false) {
							Toast.makeText(ctx, "超时", Toast.LENGTH_SHORT)
									.show();
						}
						break;
					}
				}

			});
			if (mode.equals(versionName)) {
				// 进入强制更新
				UmengUpdateAgent
						.setDialogListener(new UmengDialogButtonListener() {
							@Override
							public void onClick(int status) {

								switch (status) {
								case UpdateStatus.Update:
									final MyAlertDialog dialogCache = new MyAlertDialog(ctx)
									.builder().setMsg("正在下载").setTitle("提示")
									.setNegativeButton("退出", new OnClickListener() {
										@Override
										public void onClick(View v) {
											mlApplication.finishActivity();
										}
									});
							dialogCache.show();
							dialogCache.setCancelable(false);
									break;
								default:
									// 退出应用
									mlApplication.finishActivity();
								}
							}
						});
				break;
			}

		}
	}

	public void update() {
		UmengUpdateAgent.update(ctx);
	}
}
